﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TireService.Data;

namespace TireService.Website
{
    public partial class AnmeldService : System.Web.UI.Page
    {

        #region Public Properties

        public int ServiceCarScheduleDetailId
        {
            get
            {
                if (ViewState["_servicedetail"] != null)
                    return (int)ViewState["_servicedetail"];
                else
                    return 0;
            }
            set
            {
                ViewState["_servicedetail"] = value;
            }
        }

        public string CustomerName
        {
            get
            {
                if (ViewState["_customername"] != null)
                    return (string)ViewState["_customername"];
                else
                    return "";
            }
            set
            {
                ViewState["_customername"] = value;
            }
        }

        public string CustomerCity
        {
            get
            {
                if (ViewState["_customercity"] != null)
                    return (string)ViewState["_customercity"];
                else
                    return "";
            }
            set
            {
                ViewState["_customercity"] = value;
            }
        }

        public int CustomerZipCode
        {
            get
            {
                if (ViewState["_zipcode"] != null)
                    return (int)ViewState["_zipcode"];
                else
                    return 0;
            }
            set
            {
                ViewState["_zipcode"] = value;
            }
        }

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["ServiceId"] != null)
            {
                Guid detailguid = new Guid(Convert.ToString(Request.QueryString["ServiceId"]));

                try
                {
                    TireServiceDB db = new TireServiceDB();

                    ServiceCarScheduleDetailView detail = db.ServiceCarScheduleDetailViews.SingleOrDefault(p => p.DetailGuid == detailguid);

                    if (detail != null)
                    {
                        int CustomerId = 0;
                        try
                        {
                            CustomerId = Convert.ToInt32(Request.QueryString["CID"]);
                        }
                        catch (Exception)
                        {

                        }

                        if (CustomerId != 0 & detail.CustomerId != CustomerId)
                        {
                            throw new ArgumentException("Den valgte service kunne ikke findes.");
                        }

                        if (detail.ScheduleDate > DateTime.Now)
                        {
                            throw new ArgumentException("Den valgte service er ikke udført endnu.");
                        }

                        // Set page properties for editing
                        this.ServiceCarScheduleDetailId = detail.ServiceCarScheduleDetailId;
                        this.CustomerName = detail.Name;
                        this.CustomerZipCode = Convert.ToInt32(detail.CustomerZipCode);
                        this.CustomerCity = detail.CustomerCity;

                        // Show UI to customer
                        pApproval.Visible = true;
                        pMessage.Visible = false;
                        
                    }
                    else
                    {
                        // Scheduledetail not found
                        throw new ArgumentException("Den valgte udkørselsdato kunne ikke findes i systemet.");
                    }

                    // Dispose database connection
                    db.Dispose();
                }
                catch (Exception ex)
                {
                    // Error occured - approval could not happen
                    pApproval.Visible = false;
                    pMessage.Visible = true;

                    if ((ex) is ArgumentException)
                    {
                        // User error - arguments not valid - show error message
                        lMessage.Text = ((ArgumentException)ex).Message;
                    }
                    else
                    {
                        // System error - show general error message
                        lMessage.Text = "Fejl opstod ved godkendelse af planlagt hjulskift - venligst prøv igen eller kontakt os.";
                    }
                }
            }
        }

        /// <summary>
        /// Button function that sends email to us with request from user to be contacted
        /// </summary>
        protected void btnSendMessage_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.ServiceCarScheduleDetailId != 0)
                {
                    TireServiceDB db = new TireServiceDB();

                    // Update detail
                    ServiceCarScheduleDetail detail = db.GetServiceCarScheduleDetail(this.ServiceCarScheduleDetailId);
                    detail.CustomerRated = true;

                    // Create new rating
                    ServiceCarScheduleDetailRating rating = new ServiceCarScheduleDetailRating();
                    detail.ServiceCarScheduleDetailRatings.Add(rating);
                    rating.Rating = Convert.ToInt32(rcServiceRate.Value);
                    if (txtComment.Text != "") rating.Comment = txtComment.Text;
                    rating.Name = this.CustomerName;
                    rating.ZipCode = this.CustomerZipCode;
                    rating.City = this.CustomerCity;

                    db.AddServiceCarScheduleDetailRating(rating);

                    // Commit changes to database
                    db.SubmitChanges();

                    // Dispose connection to database
                    db.Dispose();

                    throw new ArgumentException("Mange tak for din feedback.");
                }
            }
            catch (Exception ex)
            {
                // Error occured - approval could not happen
                pApproval.Visible = false;
                pMessage.Visible = true;

                if ((ex) is ArgumentException)
                {
                    // User error - arguments not valid - show error message
                    lMessage.Text = ((ArgumentException)ex).Message;
                }
                else
                {
                    // System error - show general error message
                    lMessage.Text = "Fejl opstod ved godkendelse af planlagt hjulskift - venligst prøv igen eller kontakt os.";
                }
            }
        }
    }
}